Results

Data associated with the results presented here can be found at: https://github.com/courtois-neuromod/anat-processing/releases.

Brain

This section presents results of brain MRI data. Below are quantitative T1 values computed using the MP2RAGE and the MTsat methods. These values are averaged within the gray matter and white matter masks.

Gray matter qMRI

# Python imports 
from pathlib import Path
import plotly.graph_objects as go
import plotly.tools as tls
from plotly.offline import plot, iplot, init_notebook_mode
from plotly.validators.scatter.marker import SymbolValidator
from IPython.core.display import display, HTML
import numpy as np
import pandas as pd

# Get data from github repo
dir = Path('neuromod-anat-brain-qmri')
if dir.exists() is False:
    !wget -O neuromod-anat-brain-qmri.zip https://github.com/courtois-neuromod/anat-processing/releases/download/r20210726/neuromod-anat-brain-qmri.zip
    !unzip -j neuromod-anat-brain-qmri.zip -d ./neuromod-anat-brain-qmri/
/tmp/ipykernel_1863/897671986.py:7: DeprecationWarning:

Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display
# Read data
df = pd.read_csv("./neuromod-anat-brain-qmri/results-neuromod-anat-brain-qmri.csv", converters={'project_id': lambda x: str(x)})

# Sort data acording to subject and session columns 
df.sort_values(['subject', 'session'], ascending=[True, True]) 

# Define lists for metrics
mean_MP2RAGE_t1_matrix = []
mean_MTS_t1_matrix     = []
mean_mtr_matrix        = []
mean_mtsat_matrix      = []

# Get the values for all 4 metric [area, mean_AP, mean_RL, angle_AP]
for i in range(1, 7, 1):
    sub_values = df.loc[df['subject'] == i]
    
    mean_MP2RAGE_ses = []
    
    mean_MTS_ses     = []
    
    mean_mtr_ses     = []

    mean_mtsat_ses   = []
    
    for j in range(1, 5, 1):
        ses_values = sub_values.loc[sub_values['session'] == j]
        
        MEAN_mp2   = -100
        
        MEAN_mts   = -100
        
        MEAN_mtr   = -100
        
        MEAN_mtsat = -100

        
        for index, row in ses_values.iterrows():

            if row['acquisition'] == 'MP2RAGE' and row['metric'] == 'T1map' and row['label'] == 'GM': 
                MEAN_mp2 = row['mean']
            
            if row['acquisition'] == 'MTS' and row['metric'] == 'T1map' and row['label'] == 'GM':
                MEAN_mts = row['mean']
                
            if row['metric'] == 'MTRmap' and row['label'] == 'GM': 
                MEAN_mtr = row['mean']
                
            if row['metric'] == 'MTsat' and row['label'] == 'GM': 
                MEAN_mtsat = row['mean']
            
        # Append values to lists for sessions
        mean_MP2RAGE_ses.append(MEAN_mp2)
        
        mean_MTS_ses.append(MEAN_mts)
        
        mean_mtr_ses.append(MEAN_mtr)
        
        mean_mtsat_ses.append(MEAN_mtsat)
        
        
    # Append session lists to main matrices for each metric
    mean_MP2RAGE_t1_matrix.append(mean_MP2RAGE_ses)
    
    mean_MTS_t1_matrix.append(mean_MTS_ses)
    
    mean_mtr_matrix.append(mean_mtr_ses)
    
    mean_mtsat_matrix.append(mean_mtsat_ses)

# Add plotly 
init_notebook_mode(connected = True)
config={'showLink': False, 'displayModeBar': False}

t1 = [-0.2, -0.06, 0.08, 0.22]
t2 = [0 -0.2 + i*0.14 for i in range(0, 4)]

def get_mean(mean_matrix):
    temp = mean_matrix[::]
    mean_list = []
    for ele in temp: 
        ele = [i for i in ele if i!=-100]
        mean_list.extend(ele)
    
    mean = float('{0:.2f}'.format(np.mean(mean_list)))
    return mean

def get_std(mean_matrix):
    temp = mean_matrix[::]
    mean_list = []
    for ele in temp: 
        ele = [i for i in ele if i!=-100]
        mean_list.extend(ele)
    
    std = float('{0:.3f}'.format(np.std(mean_list)))
    return std

def get_limit_max(matrix):
    temp = matrix[::]
    mean_list = []
    for ele in temp: 
        ele = [i for i in ele if i!=-100]
        mean_list.extend(ele)
    
    return np.max(mean_list) + (np.max(mean_list)-np.min(mean_list))/4

def get_limit_min(matrix):
    temp = matrix[::]
    mean_list = []
    for ele in temp: 
        ele = [i for i in ele if i!=-100]
        mean_list.extend(ele)
    
    return np.min(mean_list) - (np.max(mean_list)-np.min(mean_list))/4

# Get different symbols (See for reference: https://plotly.com/python/marker-style/)
raw_symbols = SymbolValidator().values
namestems = []
namevariants = []
symbols = []
for i in range(0,len(raw_symbols),3):
    name = raw_symbols[i+2]
    symbols.append(raw_symbols[i])
    namestems.append(name.replace("-open", "").replace("-dot", ""))
    namevariants.append(name[len(namestems[-1]):])

# Load fancy colors
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),  
             (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),  
             (148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),  
             (227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),  
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]


# Define labels lists (just in case)
labels =["Session 1", "Session 2","Session 3","Session 4"]
labels_subjects = ['Subject 1', 'Subject 2', 'Subject 3', 'Subject 4', 'Subject 5', 'Subject 6']
labels_int = [i for i in range(1, 7)]
x_rev = labels_int[::-1]

# Def global variables for fonts 
x_label_tick_font = 13
y_label_tick_font = 13
general_font = 13

# Add first values for labels [Sub1...Sub6]
figb = go.Figure(data=go.Scatter(x=labels_int,
                                y=[-1000, -1000, -1000, -1000, -1000, -1000],
                                mode='markers',
                                showlegend=False,
                                marker_color='red'))


# Add MEAN ------ mp2rage ------ T1
for trace in range(0, len(mean_MP2RAGE_t1_matrix)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    
    if trace == 0: 
    
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_MP2RAGE_t1_matrix[trace], 
                                  mode='markers',
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i> sec" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  showlegend = True, 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name= 'T<sub>1</sub> (mp2rage)',
                                  marker_color="rgb"+str(tableau20[0])))
    else: 
        
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_MP2RAGE_t1_matrix[trace], 
                                  mode='markers',
                                  legendgroup="group1",
                                  showlegend = False, 
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i> sec" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='T<sub>1</sub> (mp2rage)',
                                  marker_color="rgb"+str(tableau20[0])))

# Add MEAN ------ mts ------ T1
for trace in range(0, len(mean_MTS_t1_matrix)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    
    if trace == 0: 
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_MTS_t1_matrix[trace], 
                                  mode='markers',
                                  legendgroup="group2",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i> sec" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  showlegend = True, 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name= 'T<sub>1</sub> (mts)',
                                  marker_symbol=symbols[5],
                                  marker_color="rgb"+str(tableau20[3])))
        
    else: 
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_MTS_t1_matrix[trace], 
                                  mode='markers',
                                  legendgroup="group2",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i> sec" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  showlegend = False, 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='T<sup>1</sup> (mts)',
                                  marker_symbol=symbols[5],
                                  marker_color="rgb"+str(tableau20[3])))

# Add MEAN ------ nAn ------ MTR
for trace in range(0, len(mean_mtr_matrix)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    
    if trace == 0: 
    
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_mtr_matrix[trace], 
                                  mode='markers',
                                  visible=False,
                                  showlegend = True, 
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i>" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='MTR',
                                  marker_color="rgb"+str(tableau20[0])))
    else: 
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_mtr_matrix[trace], 
                                  mode='markers',
                                  visible=False,
                                  showlegend = False, 
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i>" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='MTR',
                                  marker_color="rgb"+str(tableau20[0])))
            

# Add MEAN ------ nAn ------ MTsat
for trace in range(0, len(mean_mtsat_matrix)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    
    if trace == 0: 
    
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_mtsat_matrix[trace], 
                                  mode='markers',
                                  visible=False,
                                  showlegend = True, 
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i>" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='MTsat',
                                  marker_color="rgb"+str(tableau20[0])))
        
    else: 
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_mtsat_matrix[trace], 
                                  mode='markers',
                                  visible=False,
                                  showlegend = False, 
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i>" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='MTsat',
                                  marker_color="rgb"+str(tableau20[0])))
        

# Calculate means 
line_1   = get_mean(mean_MP2RAGE_t1_matrix)    # MP2RAGE_t1 --- mean 

line_2   = get_mean(mean_MTS_t1_matrix)        # MTS_t1     --- mean 

line_3   = get_mean(mean_mtr_matrix)           # MTR   --- mean

line_4   = get_mean(mean_mtsat_matrix)         # MTsat  --- mean



# Add dotted lines for first button 
figb.add_trace(go.Scatter(x=[-1, 0, 1, 2, 3, 4, 5, 6], 
                          y=[line_1]*8,
                          mode='lines',
                          name='T<sub>1</sub>(mp2rage) mean',
                          opacity=0.5, 
                          line=dict(color="rgb"+str(tableau20[0]), 
                                    width=2,
                                    dash='dot')))

figb.add_trace(go.Scatter(x=[-1, 0, 1, 2, 3, 4, 5, 6], 
                          y=[line_2]*8,
                          mode='lines',
                          name='T<sub>1</sub>(mts) mean',
                          opacity=0.5, 
                          line=dict(color="rgb"+str(tableau20[3]), 
                                    width=2,
                                    dash='dot')))

# Add dotted lines for second button 
figb.add_trace(go.Scatter(x=[-1, 0, 1, 2, 3, 4, 5, 6], 
                          y=[line_3]*8,
                          mode='lines',
                          visible=False,
                          name='MTR mean',
                          opacity=0.5, 
                          line=dict(color="rgb"+str(tableau20[0]), 
                                    width=2,
                                    dash='dot')))


# Add dotted lines for thrid button 
figb.add_trace(go.Scatter(x=[-1, 0, 1, 2, 3, 4, 5, 6], 
                          y=[line_4]*8,
                          mode='lines',
                          visible=False,
                          name='MTsat mean',
                          opacity=0.5, 
                          line=dict(color="rgb"+str(tableau20[0]), 
                                    width=2,
                                    dash='dot')))



x = [-1, 0, 1, 2, 3, 4, 5, 6]
x_rev = x[::-1]

std_1   = get_std(mean_MP2RAGE_t1_matrix)   # MP2RAGE_t1      --- std 

std_2   = get_std(mean_MTS_t1_matrix)       # MTS_t1          --- std 

std_3   = get_std(mean_mtr_matrix)          # N/A_mtr         --- std

std_4   = get_std(mean_mtsat_matrix)        # MTS_mtsat       --- std



# Add STD for 1 button
figb.add_trace(go.Scatter(
    x=x+x_rev,
    y=[line_1+std_1]*8+[line_1-std_1]*8,
    fill='toself',
    fillcolor='rgba(31, 119, 180,0.15)',
    line_color='rgba(255,255,255,0)',
    showlegend=False,
    hoverinfo='skip',
    name='MP2RAGE_t1 STD',
))

figb.add_trace(go.Scatter(
    x=x+x_rev,
    y=[line_2+std_2]*8+[line_2-std_2]*8,
    fill='toself',
    fillcolor='rgba(255, 187, 120,0.15)',
    line_color='rgba(255,255,255,0)',
    showlegend=False,
    hoverinfo='skip',
    name='MTS_t1 STD',
))

# Add STD for 2 button
figb.add_trace(go.Scatter(
    x=x+x_rev,
    y=[line_3+std_3]*8+[line_3-std_3]*8,
    fill='toself',
    visible=False,
    fillcolor='rgba(31, 119, 180,0.15)',
    line_color='rgba(255,255,255,0)',
    showlegend=False,
    hoverinfo='skip',
    name='MTR STD',
))

# Add STD for 3 button
figb.add_trace(go.Scatter(
    x=x+x_rev,
    y=[line_4+std_4]*8+[line_4-std_4]*8,
    fill='toself',
    visible=False,
    fillcolor='rgba(31, 119, 180,0.15)',
    line_color='rgba(255,255,255,0)',
    showlegend=False,
    hoverinfo='skip',
    name='MTsat STD',
))


figb.update_layout(title = 'Brain gray matter qMRI microstructure',
                   updatemenus=[
                                dict(
                                    active = 0, 
                                    x=1.23,
                                    y=0.58,
                                    direction="down",
                                    yanchor="top",
                                    buttons=list([
                                        dict(label="T<sub>1</sub>",
                                                     method="update",
                                                     args=[{"visible": [True] + [True]*12 + [False]*12 + [True]*2 + [False]*2 + [True]*2 + [False]*2},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(np.append(mean_MP2RAGE_t1_matrix, mean_MTS_t1_matrix, axis=0)), get_limit_max(np.append(mean_MP2RAGE_t1_matrix, mean_MTS_t1_matrix, axis=0))],
                                                                          title='T<sub>1</sub> [s]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                        
                                        dict(label="MTR",
                                                     method="update",
                                                     args=[{"visible": [True] + [False]*12 + [True]*6 + [False]*6 + [False]*2 + [True]*1 +[False]*1 + [False]*2 + [True]*1 +[False]*1},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(mean_mtr_matrix), get_limit_max(mean_mtr_matrix)],
                                                                          title='MTR [a.u.]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                        
                                        dict(label="MTsat",
                                                     method="update",
                                                     args=[{"visible":  [True] + [False]*18 + [True]*6 + [False]*3 + [True]*1 + [False]*3 + [True]*1},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(mean_mtsat_matrix),get_limit_max(mean_mtsat_matrix)],
                                                                          title='MTsat [a.u.]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]) ]) )],
                  title_x = 0.445, 
                  legend=dict(orientation = 'v',
                              bordercolor="Gray",
                              borderwidth=1),
                  xaxis=dict(range=[-0.45,5.45], 
                             mirror=True,
                             ticks='outside',
                             showline=True,
                             linecolor='#000',
                             tickvals = [0, 1, 2, 3, 4, 5],
                             ticktext = labels_subjects,
                             tickfont = dict(size=x_label_tick_font)),
                  yaxis_title='T<sub>1</sub> [s]',
                  yaxis=dict(range=[get_limit_min(np.append(mean_MP2RAGE_t1_matrix, mean_MTS_t1_matrix, axis=0)), get_limit_max(np.append(mean_MP2RAGE_t1_matrix, mean_MTS_t1_matrix, axis=0))], 
                             mirror=True,
                             ticks='outside', 
                             showline=True, 
                             linecolor='#000',
                             tickfont = dict(size=y_label_tick_font)),
                   annotations=[
                               dict(text="Display metric: ", 
                                     showarrow=False,
                                     x=1.25,
                                     y=0.62,
                                     xref = 'paper',
                                     yref="paper")],
                  plot_bgcolor='rgba(227,233,244, 0.5)',
                  width = 760, 
                  height = 520,
                  font = dict(size = general_font),
                  margin=go.layout.Margin(l=50,
                                         r=50,
                                         b=60,
                                        t=35))
# Plot figure
# For jupyter-book rendering --=-- jupyter-lab
plot(figb, filename = 'new-fig-2.html', config = config)
display(HTML('new-fig-2.html'))
# For local jupyter notebook --== binder session
# iplot(figb,config=config)

White matter qMRI

Spinal cord

Spinal cord cross-sectional area

# Python imports 
from pathlib import Path
import plotly.graph_objects as go
import plotly.tools as tls
from plotly.offline import plot, iplot, init_notebook_mode
from plotly.validators.scatter.marker import SymbolValidator
from IPython.core.display import display, HTML
import numpy as np
import pandas as pd

# Get data from github repo
dir = Path('spinalcord_results')
if dir.exists() is False:
    # Get data from github repo
    !wget -O spinalcord_results.zip https://github.com/courtois-neuromod/anat-processing/releases/download/r20210610/spinalcord_results.zip
    !unzip -j spinalcord_results.zip -d ./spinalcord_results/

# Add plotly 
init_notebook_mode(connected = True)
config={'showLink': False, 'displayModeBar': False}

# Load  the data
df_t1 = pd.read_csv("./spinalcord_results/csa-SC_T1w.csv", converters={'project_id': lambda x: str(x)})
df_t2 = pd.read_csv("./spinalcord_results/csa-SC_T2w.csv", converters={'project_id': lambda x: str(x)})

# Insert new columns for Subject and Session and start inserting values
df_t1.insert(0, "Subject", "Any")
df_t1.insert(1, "Session", "Any")

# Get Subject and Session from csv
for index, row in df_t1.iterrows():
    subject = int(row['Filename'].split("/")[6].split('-')[1])
    session = int(row['Filename'].split("/")[7].split("-")[1])
    df_t1.at[index, 'Subject'] =  subject
    df_t1.at[index, 'Session'] =  session

# Sort values based on Subject -- Session
df_2 = df_t1.sort_values(['Subject', 'Session'], ascending=[True, True])    

# Define lists for metrics
mean_area_matrix_1 = []
mean_ap_matrix_1 = []
mean_rl_matrix_1 = []
mean_angle_matrix_1 = []

# Get the values for all 4 metric [area, mean_AP, mean_RL, angle_AP]
for i in range(0, 6, 1):
    sub_values = df_2.loc[df_2['Subject'] == i+1]
    
    mean_area_ses = []
    
    mean_ap_ses = []

    mean_rl_ses = []

    mean_angle_ses = []
    
    for j in range(0, 4, 1):
        ses_values = sub_values.loc[sub_values['Session'] == j+1]
        
        mean_area = -100
        
        mean_ap = -100

        mean_rl = -100

        mean_angle = -100
        
        for index, row in ses_values.iterrows():
            # Read values
            MEAN_area = row['MEAN(area)']
            
            MEAN_diameter_AP = row['MEAN(diameter_AP)']

            MEAN_diameter_RL = row['MEAN(diameter_RL)']
            
            MEAN_angle_AP = row['MEAN(angle_AP)']
            
            # Store values
            mean_area = MEAN_area 
            
            mean_ap = MEAN_diameter_AP
            
            mean_rl = MEAN_diameter_RL
            
            mean_angle = MEAN_angle_AP
        
        # Append values to lists for sessions
        mean_area_ses.append(mean_area)
        
        mean_ap_ses.append(mean_ap)
        
        mean_rl_ses.append(mean_rl)

        mean_angle_ses.append(mean_angle)
        
        
    # Append session lists to main matrices for each metric
    mean_area_matrix_1.append(mean_area_ses)
    
    mean_ap_matrix_1.append(mean_ap_ses)
    
    mean_rl_matrix_1.append(mean_rl_ses)
    
    mean_angle_matrix_1.append(mean_angle_ses)

# Insert new columns for Subject and Session and start inserting values
df_t2.insert(0, "Subject", "Any")
df_t2.insert(1, "Session", "Any")

# Get Subject and Session from csv
for index, row in df_t2.iterrows():
    subject = int(row['Filename'].split("/")[6].split('-')[1])
    session = int(row['Filename'].split("/")[7].split("-")[1])
    df_t2.at[index, 'Subject'] =  subject
    df_t2.at[index, 'Session'] =  session

# Sort values based on Subject -- Session
df_3 = df_t2.sort_values(['Subject', 'Session'], ascending=[True, True])

# Define lists for metrics
mean_area_matrix_2 = []
mean_ap_matrix_2 = []
mean_rl_matrix_2 = []
mean_angle_matrix_2 = []

# Get the values for all 4 metric [area, mean_AP, mean_RL, angle_AP]
for i in range(0, 6, 1):
    sub_values = df_3.loc[df_3['Subject'] == i+1]
    
    mean_area_ses = []
    
    mean_ap_ses = []

    mean_rl_ses = []

    mean_angle_ses = []
    
    for j in range(0, 4, 1):
        ses_values = sub_values.loc[sub_values['Session'] == j+1]
        
        mean_area = -100
        
        mean_ap = -100

        mean_rl = -100

        mean_angle = -100

        
        
        for index, row in ses_values.iterrows():
            # Read values
            MEAN_area = row['MEAN(area)']
            
            MEAN_diameter_AP = row['MEAN(diameter_AP)']

            MEAN_diameter_RL = row['MEAN(diameter_RL)']
            
            MEAN_angle_AP = row['MEAN(angle_AP)']
            
            # Store values
            mean_area = MEAN_area 
            
            mean_ap = MEAN_diameter_AP
            
            mean_rl = MEAN_diameter_RL
            
            mean_angle = MEAN_angle_AP
        
        # Append values to lists for sessions
        mean_area_ses.append(mean_area)
        
        mean_ap_ses.append(mean_ap)
        
        mean_rl_ses.append(mean_rl)

        mean_angle_ses.append(mean_angle)
        
        
        
    # Append session lists to main matrices for each metric
    mean_area_matrix_2.append(mean_area_ses)
    
    mean_ap_matrix_2.append(mean_ap_ses)
    
    mean_rl_matrix_2.append(mean_rl_ses)
    
    mean_angle_matrix_2.append(mean_angle_ses)


# Functions for mean and std    
def get_mean(mean_matrix):
    temp = mean_matrix[::]
    mean_list = []
    for ele in temp: 
        ele = [i for i in ele if i!=-100]
        mean_list.extend(ele)
    
    mean = float('{0:.7f}'.format(np.mean(mean_list)))
    return mean

def get_std(mean_matrix):
    temp = mean_matrix[::]
    mean_list = []
    for ele in temp: 
        ele = [i for i in ele if i!=-100]
        mean_list.extend(ele)
    
    std = float('{0:.7f}'.format(np.std(mean_list)))
    return std

# Functions for getting y label limits
def get_limit_max(matrix):
    temp = matrix[::]
    mean_list = []
    for ele in temp: 
        ele = [i for i in ele if i!=-100]
        mean_list.extend(ele)
    
    return np.max(mean_list) + (np.max(mean_list)-np.min(mean_list))/4

def get_limit_min(matrix):
    temp = matrix[::]
    mean_list = []
    for ele in temp: 
        ele = [i for i in ele if i!=-100]
        mean_list.extend(ele)
    
    return np.min(mean_list) - (np.max(mean_list)-np.min(mean_list))/4

# Function for drawing lines 
def draw_line(mean_matrix, fig, metric, statistic, visible=False):
    # Define x values 
    x = [-1, 0, 1, 2, 3, 4, 5, 6]
    x_rev = x[::-1] # reversed list
    x_region = len(x) 
      
        
    if metric == 'T1w' or metric == 'T2w':
        if statistic == 'mean':
            # Define name and color for mean line 
            name = 'T<sub>1</sub>w mean' if metric == 'T1w' else 'T<sub>2</sub>w mean'
            color = "rgb(31, 119, 180)" if metric == 'T1w' else "rgb(255, 187, 120)"
            # Calculate the mean value
            line   = get_mean(mean_matrix)

            # Add the mean for given matrix 
            fig.add_trace(go.Scatter(x=x, 
                                     y=[line]*x_region,
                                     mode='lines',
                                     visible = visible, 
                                     name=name,
                                     opacity=0.5, 
                                     line=dict(color=color, 
                                                width=2,
                                                dash='dot')))

        elif statistic == 'std':
            # Define name and color for STD region 
            name = 'T<sub>1</sub>w STD' if metric == 'T1w' else 'T<sub>2</sub>w STD'
            fillcolor = 'rgba(31, 119, 180,0.15)' if metric == 'T1w' else 'rgba(255, 187, 120,0.15)'
            # Calculate the mean, std value
            line = get_mean(mean_matrix)
            std  = get_std(mean_matrix) 

            # Add STD for given matrix
            fig.add_trace(go.Scatter(
                x=x+x_rev,
                y=[line+std]*x_region+[line-std]*x_region,
                fill='toself',
                visible = visible,
                fillcolor=fillcolor,
                line_color='rgba(255,255,255,0)',
                showlegend=False,
                hoverinfo='skip',
                name=name,
            ))
        
    else:
        if statistic == 'mean':
            # Define name and color for mean line 
            name = metric + " mean"
            color = "rgb(31, 119, 180)" 
            # Calculate the mean value
            line   = get_mean(mean_matrix)

            # Add the mean for given matrix 
            fig.add_trace(go.Scatter(x=x, 
                                     y=[line]*x_region,
                                     mode='lines',
                                     visible = visible, 
                                     name=name,
                                     opacity=0.5, 
                                     line=dict(color=color, 
                                                width=2,
                                                dash='dot')))
        elif statistic == 'std':
            # Define name and color for STD region 
            name = metric + " STD"
            fillcolor = 'rgba(31, 119, 180,0.15)' 
            # Calculate the mean, std value
            line = get_mean(mean_matrix)
            std  = get_std(mean_matrix) 

            # Add STD for given matrix
            fig.add_trace(go.Scatter(
                x=x+x_rev,
                y=[line+std]*x_region+[line-std]*x_region,
                fill='toself',
                visible = visible,
                fillcolor=fillcolor,
                line_color='rgba(255,255,255,0)',
                showlegend=False,
                hoverinfo='skip',
                name=name,
            ))   
            

# Get different symbols (See for reference: https://plotly.com/python/marker-style/)
raw_symbols = SymbolValidator().values
namestems = []
namevariants = []
symbols = []
for i in range(0,len(raw_symbols),3):
    name = raw_symbols[i+2]
    symbols.append(raw_symbols[i])
    namestems.append(name.replace("-open", "").replace("-dot", ""))
    namevariants.append(name[len(namestems[-1]):])

# Load fancy colors
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),  
             (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),  
             (148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),  
             (227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),  
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]



# Define lists needed for plotting 
t1 = [-0.2, -0.06, 0.08, 0.22]
t2 = [0 -0.2 + i*0.14 for i in range(0, 4)]
labels =["Session 1", "Session 2","Session 3","Session 4"]
labels_subjects = ['Subject 1', 'Subject 2', 'Subject 3', 'Subject 4', 'Subject 5', 'Subject 6']
labels_int = [i for i in range(1, 7)]
x_rev = labels_int[::-1]

# Def global variables for fonts 
x_label_tick_font = 13
y_label_tick_font = 13
general_font = 13


# Add first values for labels [Sub1...Sub6]
figb = go.Figure(data=go.Scatter(x=labels_int,
                                y=[-1000, -1000, -1000, -1000, -1000, -1000],
                                mode='markers',
                                showlegend=False,
                                marker_color='red'))


# Add MEAN [area] --- > T1w
for trace in range(0, len(mean_area_matrix_1)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False
    figb.add_trace(go.Scatter(x=t, 
                              y=mean_area_matrix_1[trace], 
                              mode='markers',
                              legendgroup="group1",
                              hovertemplate = 
                              "Mean (area): <i> %{y: .2f} </i> mm<sup>2</sup>" + 
                              "<br>" + 
                              "<b>%{text}</b>", 
                              showlegend = showlegend, 
                              text = ['Session {}'.format(i + 1) for i in range(4)],
                              name= 'T<sub>1</sub>w',
                              marker_color="rgb"+str(tableau20[0])))
    
# Add MEAN [area] --- > T2w
for trace in range(0, len(mean_area_matrix_2)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False
    figb.add_trace(go.Scatter(x=t, 
                                  y=mean_area_matrix_2[trace], 
                                  mode='markers',
                                  legendgroup="group2",
                                  hovertemplate = 
                                  "Mean (area): <i> %{y: .2f} </i> mm<sup>2</sup>" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  showlegend = showlegend, 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name= 'T<sub>2</sub>w',
                                  marker_symbol=symbols[5],
                                  marker_color="rgb"+str(tableau20[3])))
        

# Add MEAN [AP] --- > T1w
for trace in range(0, len(mean_ap_matrix_1)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace == 0 else False
    figb.add_trace(go.Scatter(x=t, 
                              y=mean_ap_matrix_1[trace], 
                              mode='markers',
                              visible=False,
                              showlegend = showlegend, 
                              legendgroup="group1",
                              hovertemplate = 
                              "Mean [AP]: <i> %{y: .2f} </i> mm" + 
                              "<br>" + 
                              "<b>%{text}</b>", 
                              text = ['Session {}'.format(i + 1) for i in range(4)],
                              name='T<sub>1</sub>w',
                              marker_color="rgb"+str(tableau20[0])))

            

# Add MEAN [AP] --- > T2w
for trace in range(0, len(mean_ap_matrix_2)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False
    figb.add_trace(go.Scatter(x=t, 
                                  y=mean_ap_matrix_2[trace], 
                                  mode='markers',
                                  visible=False,
                                  showlegend = showlegend, 
                                  legendgroup="group2",
                                  hovertemplate = 
                                  "Mean [AP]: <i> %{y: .2f} </i> mm" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  marker_symbol=symbols[5],
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='T<sub>2</sub>w',
                                  marker_color="rgb"+str(tableau20[3])))

    
# Add MEAN [RL] --- > T1w
for trace in range(0, len(mean_rl_matrix_1)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False
    figb.add_trace(go.Scatter(x=t, 
                              y=mean_rl_matrix_1[trace], 
                              mode='markers',
                              visible=False,
                              showlegend = showlegend, 
                              legendgroup="group1",
                              hovertemplate = 
                              "Mean (RL): <i> %{y: .2f} </i> mm" + 
                              "<br>" + 
                              "<b>%{text}</b>", 
                              text = ['Session {}'.format(i + 1) for i in range(4)],
                              name='T<sub>1</sub>w',
                              marker_color="rgb"+str(tableau20[0])))
        
        
    
# Add MEAN [RL] --- > T2w
for trace in range(0, len(mean_rl_matrix_2)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False
    figb.add_trace(go.Scatter(x=t, 
                                  y=mean_rl_matrix_2[trace], 
                                  mode='markers',
                                  visible=False,
                                  showlegend = showlegend, 
                                  legendgroup="group2",
                                  hovertemplate = 
                                  "Mean (RL): <i> %{y: .2f} </i> mm" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='T<sub>2</sub>w',
                                  marker_symbol=symbols[5],
                                  marker_color="rgb"+str(tableau20[3])))

# Add MEAN [angle_AP] --- > T1w
for trace in range(0, len(mean_angle_matrix_1)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False
    figb.add_trace(go.Scatter(x=t, 
                                  y=mean_angle_matrix_1[trace], 
                                  mode='markers',
                                  visible=False,
                                  showlegend = showlegend, 
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "Mean (angle_AP): <i> %{y: .2f} </i>°" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='T<sub>1</sub>w',
                                  marker_color="rgb"+str(tableau20[0])))
        
    
# Add MEAN [angle_AP] --- > T2w
for trace in range(0, len(mean_angle_matrix_2)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False
    figb.add_trace(go.Scatter(x=t, 
                              y=mean_angle_matrix_2[trace], 
                              mode='markers',
                              visible=False,
                              showlegend = showlegend, 
                              legendgroup="group2",
                              hovertemplate = 
                              "Mean (angle_AP): <i> %{y: .2f} </i>°" + 
                              "<br>" + 
                              "<b>%{text}</b>", 
                              text = ['Session {}'.format(i + 1) for i in range(4)],
                              name='T<sub>2</sub>w',
                              marker_symbol=symbols[5],
                              marker_color="rgb"+str(tableau20[3])))
        
# Calculate means 
draw_line(mean_area_matrix_1, figb, 'T1w', 'mean', visible=True)    # T1w mean area --- mean 
draw_line(mean_area_matrix_2, figb, 'T2w', 'mean', visible=True)    # T2w mean area --- mean 

draw_line(mean_ap_matrix_1, figb, 'T1w', 'mean')                    # T1w mean AP   --- mean
draw_line(mean_ap_matrix_2, figb, 'T2w', 'mean')                    # T2w mean AP   --- mean

draw_line(mean_rl_matrix_1, figb, 'T1w', 'mean')                    # T1w mean RL  --- mean
draw_line(mean_rl_matrix_2, figb, 'T2w', 'mean')                    # T2w mean RL  --- mean

draw_line(mean_angle_matrix_1, figb, 'T1w', 'mean')                 # T1w angle    --- mean
draw_line(mean_angle_matrix_2, figb, 'T2w', 'mean')                 # T2w angle    --- mean


draw_line(mean_area_matrix_1, figb, 'T1w', 'std', visible=True)      # T1w mean area --- std 
draw_line(mean_area_matrix_2, figb, 'T2w', 'std', visible=True)     # T2w mean area --- std 

draw_line(mean_ap_matrix_1, figb, 'T1w', 'std')                    # T1w mean AP   --- std
draw_line(mean_ap_matrix_2, figb, 'T2w', 'std')                    # T2w mean AP   --- std

draw_line(mean_rl_matrix_1, figb, 'T1w', 'std')                    # T1w mean RL  --- std
draw_line(mean_rl_matrix_2, figb, 'T2w', 'std')                    # T2w mean RL  --- std

draw_line(mean_angle_matrix_1, figb, 'T1w', 'std')                 # T1w angle    --- std
draw_line(mean_angle_matrix_2, figb, 'T2w', 'std')                 # T2w angle    --- std


figb.update_layout(title = '  Spinal cord CSA [T<sub>1</sub>w/T<sub>2</sub>w]',
                   updatemenus=[
                                dict(
                                    active = 0, 
                                    x=1.27,
                                    y=0.58,
                                    direction="down",
                                    yanchor="top",
                                    buttons=list([
                                        dict(label="Mean (area)",
                                                     method="update",
                                                     args=[{"visible": [True] + [True]*12 + [False]*36 + [True]*2 + [False]*6 + [True]*2 + [False]*6},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(np.append(mean_area_matrix_1, mean_area_matrix_2, axis=0)), get_limit_max(np.append(mean_area_matrix_1, mean_area_matrix_2, axis=0))],
                                                                          title='Mean (area) [mm<sup>2</sup>]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                        
                                        dict(label="Mean (AP)",
                                                     method="update",
                                                     args=[{"visible": [True] + [False]*12 + [True]*12 + [False]*24 + [False]*2 + [True]*2 +[False]*4 + [False]*2 + [True]*2 +[False]*4},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(np.append(mean_ap_matrix_1, mean_ap_matrix_2, axis=0)), get_limit_max(np.append(mean_ap_matrix_1, mean_ap_matrix_2, axis=0))],
                                                                          title='Mean (AP) [mm]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                    
                                        dict(label="Mean (RL)",
                                                     method="update",
                                                     args=[{"visible": [True] + [False]*24 + [True]*12 + [False]*12 + [False]*4 + [True]*2 +[False]*2 + [False]*4 + [True]*2 +[False]*2},
                                                    
                                                           {"yaxis": dict(range=[get_limit_min(np.append(mean_rl_matrix_1, mean_rl_matrix_2, axis=0)), get_limit_max(np.append(mean_rl_matrix_1, mean_rl_matrix_2, axis=0))],
                                                                          title='Mean (RL) [mm]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                        
                                        dict(label="Mean (angle)",
                                                     method="update",
                                                     args=[{"visible":  [True] + [False]*36 + [True]*12 + [False]*6 + [True]*2 + [False]*6 + [True]*2 },
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(np.append(mean_angle_matrix_1, mean_angle_matrix_2, axis=0)), get_limit_max(np.append(mean_angle_matrix_1, mean_angle_matrix_2, axis=0))],
                                                                          title='Mean (angle) [°]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]) ]) )],
                  title_x = 0.445, 
                  legend=dict(orientation = 'v',
                              bordercolor="Gray",
                              borderwidth=1),
                  xaxis=dict(range=[-0.45,5.45], 
                             mirror=True,
                             ticks='outside',
                             showline=True,
                             linecolor='#000',
                             tickvals = [0, 1, 2, 3, 4, 5],
                             ticktext = labels_subjects,
                             tickfont = dict(size=x_label_tick_font)),
                  yaxis_title='Mean (area) [mm<sup>2</sup>]',
                  yaxis=dict(range=[get_limit_min(np.append(mean_area_matrix_1, mean_area_matrix_2, axis=0)), get_limit_max(np.append(mean_area_matrix_1, mean_area_matrix_2, axis=0))], 
                             mirror=True,
                             ticks='outside', 
                             showline=True, 
                             linecolor='#000',
                             tickfont = dict(size=y_label_tick_font)),
                   annotations=[
                               dict(text="Display metric: ", 
                                     showarrow=False,
                                     x=1.25,
                                     y=0.62,
                                     xref = 'paper',
                                     yref="paper")],
                  plot_bgcolor='rgba(227,233,244, 0.5)',
                  width = 760, 
                  height = 520,
                  font = dict(size = general_font),
                  margin=go.layout.Margin(l=50,
                                         r=50,
                                         b=60,
                                        t=35))
# Plot figure
# For jupyter-book rendering --=-- jupyter-lab
plot(figb, filename = 'new-fig.html', config = config)
display(HTML('new-fig.html'))
# For local jupyter notebook --== binder session
# iplot(figb,config=config)
/tmp/ipykernel_1863/3643689153.py:7: DeprecationWarning:

Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display

Gray matter cross-sectional area

import plotly.graph_objects as go
import plotly.tools as tls
from plotly.offline import plot, iplot, init_notebook_mode
from plotly.validators.scatter.marker import SymbolValidator
from IPython.core.display import display, HTML
import numpy as np
import pandas as pd
# Add plotly 
init_notebook_mode(connected = True)
config={'showLink': False, 'displayModeBar': False}

# Load  the data
df_3 = pd.read_csv("./spinalcord_results/csa-GM_T2s.csv", converters={'project_id': lambda x: str(x)})

# Insert new columns for Subject and Session and start inserting values
df_3.insert(0, "Subject", "Any")
df_3.insert(1, "Session", "Any")

# Get Subject and Session from csv
for index, row in df_3.iterrows():
    subject = int(row['Filename'].split("/")[6].split('-')[1])
    session = int(row['Filename'].split("/")[7].split("-")[1])
    df_3.at[index, 'Subject'] =  subject
    df_3.at[index, 'Session'] =  session

# Sort values based on Subject -- Session
df_3 = df_3.sort_values(['Subject', 'Session'], ascending=[True, True])   

# Define lists for metrics
mean_area_matrix_1 = []


# Get the values for all 4 metric [area, mean_AP, mean_RL, angle_AP]
for i in range(0, 6, 1):
    sub_values = df_3.loc[df_3['Subject'] == i+1]
    
    mean_area_ses = []

    for j in range(0, 4, 1):
        ses_values = sub_values.loc[sub_values['Session'] == j+1]
        mean_area = -100

        for index, row in ses_values.iterrows():
            # Read values
            MEAN_area = row['MEAN(area)']
            # Store values
            mean_area = MEAN_area 
        
        # Append values to lists for sessions
        mean_area_ses.append(mean_area)
        
    # Append session lists to main matrices for each metric
    mean_area_matrix_1.append(mean_area_ses)
    
# Add first values for labels [Sub1...Sub6]
figb = go.Figure(data=go.Scatter(x=labels_int,
                                y=[-1000, -1000, -1000, -1000, -1000, -1000],
                                mode='markers',
                                showlegend=False,
                                marker_color='red'))


# Add MEAN [area] --- > T1w
for trace in range(0, len(mean_area_matrix_1)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    
    showlegend = True if trace ==0 else False

    figb.add_trace(go.Scatter(x=t, 
                              y=mean_area_matrix_1[trace], 
                              mode='markers',
                              legendgroup="group1",
                              hovertemplate = 
                              "Mean (area): <i> %{y: .2f} </i> mm<sup>2</sup>" + 
                              "<br>" + 
                              "<b>%{text}</b>", 
                              showlegend = showlegend, 
                              text = ['Session {}'.format(i + 1) for i in range(4)],
                              name= 'T<sub>2</sub>s',
                              marker_color="rgb"+str(tableau20[0])))


# Calculate mean and std
draw_line(mean_area_matrix_1, figb, 'T<sub>2</sub>s', 'mean', visible=True) # T2s mean area --- mean 
draw_line(mean_area_matrix_1, figb, 'T<sub>2</sub>s', 'std', visible=True)  # T2s mean area --- std 


figb.update_layout(title = 'Spinal cord gray matter CSA',
                  title_x = 0.5, 
                  showlegend=False,
                  xaxis=dict(range=[-0.45,5.45], 
                             mirror=True,
                             ticks='outside',
                             showline=True,
                             linecolor='#000',
                             tickvals = [0, 1, 2, 3, 4, 5],
                             ticktext = labels_subjects,
                             tickfont = dict(size=x_label_tick_font)),
                  yaxis_title='Mean (area) [mm<sup>2</sup>]',
                  yaxis=dict(range=[get_limit_min(mean_area_matrix_1), get_limit_max(mean_area_matrix_1)], 
                             mirror=True,
                             ticks='outside', 
                             showline=True, 
                             linecolor='#000',
                             tickfont = dict(size=y_label_tick_font)),
                  plot_bgcolor='rgba(227,233,244, 0.5)',
                  width = 640, 
                  height = 520,
                  font = dict(size = general_font),
                  margin=go.layout.Margin(l=50,
                                         r=50,
                                         b=60,
                                        t=35))
# Plot figure
# For jupyter-book rendering --=-- jupyter-lab
plot(figb, filename = 'new-fig-2.html', config = config)
display(HTML('new-fig-2.html'))
# For local jupyter notebook --== binder session
# iplot(figb,config=config)
/tmp/ipykernel_1863/582358489.py:5: DeprecationWarning:

Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display

White matter qMRI

import plotly.graph_objects as go
import plotly.tools as tls
from plotly.offline import plot, iplot, init_notebook_mode
from plotly.validators.scatter.marker import SymbolValidator
from IPython.core.display import display, HTML
import numpy as np
import pandas as pd

# Add plotly 
init_notebook_mode(connected = True)
config={'showLink': False, 'displayModeBar': False}

# Read data for (DWI_FA, DWI_MD, DWI_RD, MTR, MTsat, T1)
df_dwi_fa = pd.read_csv("./spinalcord_results/DWI_FA.csv", converters={'project_id': lambda x: str(x)})
df_dwi_md = pd.read_csv("./spinalcord_results/DWI_MD.csv", converters={'project_id': lambda x: str(x)})
df_dwi_rd = pd.read_csv("./spinalcord_results/DWI_RD.csv", converters={'project_id': lambda x: str(x)})
df_mtr    = pd.read_csv("./spinalcord_results/MTR.csv", converters={'project_id': lambda x: str(x)})
df_mtsat  = pd.read_csv("./spinalcord_results/MTsat.csv", converters={'project_id': lambda x: str(x)})
df_t1_2   = pd.read_csv("./spinalcord_results/T1.csv", converters={'project_id': lambda x: str(x)})

# Function for reading session values 
def get_sessions_values(df, metric):
    '''

    :param df: pandas dataframe with results
    :param metric: column in dataframe
    :return: session values as matrix 
    '''
    df.insert(0, "Subject", "Any")
    df.insert(1, "Session", "Any")

    # Get Subject and Session from csv
    for index, row in df.iterrows():
        subject = int(row['Filename'].split("/")[6].split('-')[1])
        session = int(row['Filename'].split("/")[7].split("-")[1])
        df.at[index, 'Subject'] = subject
        df.at[index, 'Session'] = session

    # Sort values based on Subject -- Session
    df_2 = df.sort_values(['Subject', 'Session'], ascending=[True, True])

    # Define lists for metrics
    matrix_1 = []

    # Get the values for all desired column (metric)
    for i in range(0, 6, 1):
        sub_values = df_2.loc[df_2['Subject'] == i + 1]
        ses = []

        for j in range(0, 4, 1):
            ses_values = sub_values.loc[sub_values['Session'] == j + 1]

            temp = -100

            for index, row in ses_values.iterrows():
                # Read values
                my_value = row[metric]

                # Store values
                temp = my_value

                # Append values to lists for sessions
            ses.append(temp)

        # Append session lists to main matrices for each metric
        matrix_1.append(ses)

    return matrix_1

# Get values for sessions, store in lists 
matrix_dwi_fa = get_sessions_values(df=df_dwi_fa,metric="WA()")
matrix_dwi_md = get_sessions_values(df=df_dwi_md, metric="WA()")
matrix_dwi_rd = get_sessions_values(df=df_dwi_rd, metric="WA()")
matrix_mtr    = get_sessions_values(df=df_mtr, metric="WA()")
matrix_mtsat  = get_sessions_values(df=df_mtsat, metric="WA()")
matrix_t1_2   = get_sessions_values(df=df_t1_2, metric="WA()")


# Get different symbols (See for reference: https://plotly.com/python/marker-style/)
raw_symbols = SymbolValidator().values
namestems = []
namevariants = []
symbols = []
for i in range(0,len(raw_symbols),3):
    name = raw_symbols[i+2]
    symbols.append(raw_symbols[i])
    namestems.append(name.replace("-open", "").replace("-dot", ""))
    namevariants.append(name[len(namestems[-1]):])

# Load fancy colors
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),  
             (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),  
             (148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),  
             (227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),  
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]


# Define lists needed for plotting 
t1 = [-0.2, -0.06, 0.08, 0.22]
t2 = [0 -0.2 + i*0.14 for i in range(0, 4)]
labels =["Session 1", "Session 2","Session 3","Session 4"]
labels_subjects = ['Subject 1', 'Subject 2', 'Subject 3', 'Subject 4', 'Subject 5', 'Subject 6']
labels_int = [i for i in range(1, 7)]
x_rev = labels_int[::-1]

# Add first values for labels [Sub1...Sub6]
figb = go.Figure(data=go.Scatter(x=labels_int,
                                y=[-1000, -1000, -1000, -1000, -1000, -1000],
                                mode='markers',
                                showlegend=False,
                                marker_color='red'))
# Add DWI_FA 
for trace in range(0, len(matrix_dwi_fa)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False 
    figb.add_trace(go.Scatter(x=t, 
                              y=matrix_dwi_fa[trace], 
                              mode='markers',
                              legendgroup="group1",
                              hovertemplate = 
                              "<i> %{y: .2f} </i>" + 
                              "<br>" + 
                              "<b>%{text}</b>", 
                              showlegend = showlegend, 
                              text = ['Session {}'.format(i + 1) for i in range(4)],
                              name= 'DWI_FA',
                              marker_color="rgb"+str(tableau20[0])))


# Add DWI_MD
for trace in range(0, len(matrix_dwi_md)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False 
    figb.add_trace(go.Scatter(x=t, 
                                  y=matrix_dwi_md[trace], 
                                  mode='markers',
                                  visible=False,
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "<i> %{y: .6f} </i> mm<sup>2</sup>/s" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  showlegend = showlegend, 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name= 'DWI_MD',
                                  marker_color="rgb"+str(tableau20[0])))


        
# Add DWI_RD
for trace in range(0, len(matrix_dwi_rd)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False 
    figb.add_trace(go.Scatter(x=t, 
                                  y=matrix_dwi_rd[trace], 
                                  mode='markers',
                                  visible=False,
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "<i> %{y: .6f} </i> mm<sup>2</sup>/s" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  showlegend = showlegend, 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name= 'DWI_RD',
                                  marker_color="rgb"+str(tableau20[0])))


        
# Add MTR
for trace in range(0, len(matrix_mtr)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False  
    figb.add_trace(go.Scatter(x=t, 
                              y=matrix_mtr[trace], 
                              mode='markers',
                              visible=False,
                              legendgroup="group1",
                              hovertemplate = 
                              "<i> %{y: .2f} </i>" + 
                              "<br>" + 
                              "<b>%{text}</b>", 
                              showlegend = showlegend, 
                              text = ['Session {}'.format(i + 1) for i in range(4)],
                              name= 'MTR',
                              marker_color="rgb"+str(tableau20[0])))


# Add MTsat
for trace in range(0, len(matrix_mtsat)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False 
    figb.add_trace(go.Scatter(x=t, 
                              y=matrix_mtsat[trace], 
                              mode='markers',
                              visible=False,
                              legendgroup="group1",
                              hovertemplate = 
                              "<i> %{y: .2f} </i>" + 
                              "<br>" + 
                              "<b>%{text}</b>", 
                              showlegend = showlegend, 
                              text = ['Session {}'.format(i + 1) for i in range(4)],
                              name= 'MTsat',
                              marker_color="rgb"+str(tableau20[0])))

# Add T1
for trace in range(0, len(matrix_t1_2)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    showlegend = True if trace ==0 else False 
    figb.add_trace(go.Scatter(x=t, 
                              y=matrix_t1_2[trace], 
                              mode='markers',
                              visible=False,
                              legendgroup="group1",
                              hovertemplate = 
                              "<i> %{y: .7f} </i> sec" + 
                              "<br>" + 
                              "<b>%{text}</b>", 
                              showlegend = showlegend, 
                              text = ['Session {}'.format(i + 1) for i in range(4)],
                              name= 'T<sub>1</sub>',
                              marker_color="rgb"+str(tableau20[0])))
        
        
# Calculate means 
draw_line(matrix_dwi_fa, figb, 'DWI_FA', 'mean', visible=True)      # DWI_FA  --- mean 
draw_line(matrix_dwi_md, figb, 'DWI_MD', 'mean')                    # DWI_MD  --- mean 
draw_line(matrix_dwi_rd, figb, 'DWI_RD', 'mean')                    # DWI_RD  --- mean
draw_line(matrix_mtr, figb, 'MTR', 'mean')                          # MTR     --- mean
draw_line(matrix_mtsat, figb, 'MTsat', 'mean')                      # MTsat   --- mean
draw_line(matrix_t1_2, figb, 'T<sub>1</sub>', 'mean')               # T1      --- mean

# Calculate stds
draw_line(matrix_dwi_fa, figb, 'DWI_FA', 'std', visible=True)       # DWI_FA  --- mean 
draw_line(matrix_dwi_md, figb, 'DWI_MD', 'std')                     # DWI_MD  --- mean 
draw_line(matrix_dwi_rd, figb, 'DWI_RD', 'std')                     # DWI_RD  --- mean
draw_line(matrix_mtr, figb, 'MTR', 'std')                           # MTR     --- mean
draw_line(matrix_mtsat, figb, 'MTsat', 'std')                       # MTsat   --- mean
draw_line(matrix_t1_2, figb, 'T<sub>1</sub>', 'std')                # T1      --- mean


figb.update_layout(title = 'Spinal cord qMRI microstructure ',
                   updatemenus=[
                                dict(
                                    active = 0, 
                                    x=1.24,
                                    y=0.58,
                                    direction="down",
                                    yanchor="top",
                                    buttons=list([
                                        dict(label="DWI_FA",
                                                     method="update",
                                                     args=[{"visible": [True] + [True]*6 + [False]*30 + [True]*1 + [False]*5 + [True]*1 + [False]*5},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(matrix_dwi_fa), get_limit_max(matrix_dwi_fa)],
                                                                          title='DWI_FA [a.u.]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                        
                                        dict(label="DWI_MD",
                                                     method="update",
                                                     args=[{"visible": [True] + [False]*6 + [True]*6 + [False]*24 + [False]*1 + [True]*1 +[False]*4 + [False]*1 + [True]*1 +[False]*4},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(matrix_dwi_md), get_limit_max(matrix_dwi_md)],
                                                                          title='DWI_MD [mm<sup>2</sup>/s]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                        
                                        dict(label="DWI_RD",
                                                     method="update",
                                                     args=[{"visible": [True] + [False]*12 + [True]*6 + [False]*18 + [False]*2 + [True]*1 +[False]*3 + [False]*2 + [True]*1 +[False]*3},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(matrix_dwi_rd), get_limit_max(matrix_dwi_rd)],
                                                                          title=' DWI_RD [mm<sup>2</sup>/s]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                        dict(label="MTR",
                                                     method="update",
                                                     args=[{"visible": [True] + [False]*18 + [True]*6 + [False]*12 + [False]*3 + [True]*1 +[False]*2 + [False]*3 + [True]*1 +[False]*2},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(matrix_mtr), get_limit_max(matrix_mtr)],
                                                                          title='MTR [a.u.]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                    
                                        dict(label="MTsat",
                                                     method="update",
                                                     args=[{"visible": [True] + [False]*24 + [True]*6 + [False]*6 + [False]*4 + [True]*1 +[False]*1 + [False]*4 + [True]*1 +[False]*1},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(matrix_mtsat), get_limit_max(matrix_mtsat)],
                                                                          title='MTsat [a.u.]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                        
                                        dict(label="T<sub>1</sub>",
                                                     method="update",
                                                     args=[{"visible":  [True] + [False]*30 + [True]*6 + [False]*5 + [True]*1 + [False]*5 + [True]*1 },
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(matrix_t1_2), get_limit_max(matrix_t1_2)],
                                                                          title='T<sub>1</sub> [s]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]) ]) )],
                  title_x = 0.445, 
                  legend=dict(orientation = 'v',
                              bordercolor="Gray",
                              borderwidth=1),
                  xaxis=dict(range=[-0.45,5.45], 
                             mirror=True,
                             ticks='outside',
                             showline=True,
                             linecolor='#000',
                             tickvals = [0, 1, 2, 3, 4, 5],
                             ticktext = labels_subjects,
                             tickfont = dict(size=x_label_tick_font)),
                  yaxis_title='DWI_FA [a.u.]',
                  yaxis=dict(range=[get_limit_min(matrix_dwi_fa), get_limit_max(matrix_dwi_fa)], 
                             mirror=True,
                             ticks='outside', 
                             showline=True, 
                             linecolor='#000',
                             tickfont = dict(size=y_label_tick_font)),
                   annotations=[
                               dict(text="Display metric: ", 
                                     showarrow=False,
                                     x=1.25,
                                     y=0.62,
                                     xref = 'paper',
                                     yref="paper")],
                  plot_bgcolor='rgba(227,233,244, 0.5)',
                  width = 760, 
                  height = 520,
                  font = dict(size = general_font),
                  margin=go.layout.Margin(l=50,
                                         r=50,
                                         b=60,
                                        t=35))
# Plot figure
# For jupyter-book rendering --=-- jupyter-lab
plot(figb, filename = 'new-fig-3.html', config = config)
display(HTML('new-fig-3.html'))
# For local jupyter notebook --== binder session
# iplot(figb,config=config)
/tmp/ipykernel_1863/3287582139.py:5: DeprecationWarning:

Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display
import plotly.graph_objects as go
import plotly.tools as tls
from plotly.offline import plot, iplot, init_notebook_mode
from plotly.validators.scatter.marker import SymbolValidator
from IPython.core.display import display, HTML
import numpy as np
import pandas as pd

# Read data
df = pd.read_csv("./neuromod-anat-brain-qmri/results-neuromod-anat-brain-qmri.csv", converters={'project_id': lambda x: str(x)})

# Sort data acording to subject and session columns 
df.sort_values(['subject', 'session'], ascending=[True, True]) 

# Define lists for metrics
mean_MP2RAGE_t1_matrix = []
mean_MTS_t1_matrix     = []
mean_mtr_matrix        = []
mean_mtsat_matrix      = []

# Get the values for all 4 metric [area, mean_AP, mean_RL, angle_AP]
for i in range(1, 7, 1):
    sub_values = df.loc[df['subject'] == i]
    
    mean_MP2RAGE_ses = []
    
    mean_MTS_ses     = []
    
    mean_mtr_ses     = []

    mean_mtsat_ses   = []
    
    for j in range(1, 5, 1):
        ses_values = sub_values.loc[sub_values['session'] == j]
        
        MEAN_mp2   = -100
        
        MEAN_mts   = -100
        
        MEAN_mtr   = -100
        
        MEAN_mtsat = -100

        
        for index, row in ses_values.iterrows():

            if row['acquisition'] == 'MP2RAGE' and row['metric'] == 'T1map' and row['label'] == 'WM': 
                MEAN_mp2 = row['mean']
            
            if row['acquisition'] == 'MTS' and row['metric'] == 'T1map' and row['label'] == 'WM':
                MEAN_mts = row['mean']
                
            if row['metric'] == 'MTRmap' and row['label'] == 'WM': 
                MEAN_mtr = row['mean']
                
            if row['metric'] == 'MTsat' and row['label'] == 'WM': 
                MEAN_mtsat = row['mean']
            
        # Append values to lists for sessions
        mean_MP2RAGE_ses.append(MEAN_mp2)
        
        mean_MTS_ses.append(MEAN_mts)
        
        mean_mtr_ses.append(MEAN_mtr)
        
        mean_mtsat_ses.append(MEAN_mtsat)
        
        
    # Append session lists to main matrices for each metric
    mean_MP2RAGE_t1_matrix.append(mean_MP2RAGE_ses)
    
    mean_MTS_t1_matrix.append(mean_MTS_ses)
    
    mean_mtr_matrix.append(mean_mtr_ses)
    
    mean_mtsat_matrix.append(mean_mtsat_ses)

# Add plotly 
init_notebook_mode(connected = True)
config={'showLink': False, 'displayModeBar': False}

t1 = [-0.2, -0.06, 0.08, 0.22]
t2 = [0 -0.2 + i*0.14 for i in range(0, 4)]

def get_mean(mean_matrix):
    temp = mean_matrix[::]
    mean_list = []
    for ele in temp: 
        ele = [i for i in ele if i!=-100]
        mean_list.extend(ele)
    
    mean = float('{0:.2f}'.format(np.mean(mean_list)))
    return mean

def get_std(mean_matrix):
    temp = mean_matrix[::]
    mean_list = []
    for ele in temp: 
        ele = [i for i in ele if i!=-100]
        mean_list.extend(ele)
    
    std = float('{0:.3f}'.format(np.std(mean_list)))
    return std

# Get different symbols (See for reference: https://plotly.com/python/marker-style/)
raw_symbols = SymbolValidator().values
namestems = []
namevariants = []
symbols = []
for i in range(0,len(raw_symbols),3):
    name = raw_symbols[i+2]
    symbols.append(raw_symbols[i])
    namestems.append(name.replace("-open", "").replace("-dot", ""))
    namevariants.append(name[len(namestems[-1]):])

# Load fancy colors
tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),  
             (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),  
             (148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),  
             (227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),  
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]


# Define labels lists (just in case)
labels =["Session 1", "Session 2","Session 3","Session 4"]
labels_subjects = ['Subject 1', 'Subject 2', 'Subject 3', 'Subject 4', 'Subject 5', 'Subject 6']
labels_int = [i for i in range(1, 7)]
x_rev = labels_int[::-1]

# Def global variables for font sizes
x_label_tick_font = 13
y_label_tick_font = 13
general_font = 13

# Add first values for labels [Sub1...Sub6]
figb = go.Figure(data=go.Scatter(x=labels_int,
                                y=[-1000, -1000, -1000, -1000, -1000, -1000],
                                mode='markers',
                                showlegend=False,
                                marker_color='red'))


# Add MEAN ------ mp2rage ------ T1
for trace in range(0, len(mean_MP2RAGE_t1_matrix)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    
    if trace == 0: 
    
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_MP2RAGE_t1_matrix[trace], 
                                  mode='markers',
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i> sec" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  showlegend = True, 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name= 'T<sub>1</sub> (mp2rage)',
                                  marker_color="rgb"+str(tableau20[0])))
    else: 
        
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_MP2RAGE_t1_matrix[trace], 
                                  mode='markers',
                                  legendgroup="group1",
                                  showlegend = False, 
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i> sec" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='T<sub>1</sub> (mp2rage)',
                                  marker_color="rgb"+str(tableau20[0])))

# Add MEAN ------ mts ------ T1
for trace in range(0, len(mean_MTS_t1_matrix)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    
    if trace == 0: 
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_MTS_t1_matrix[trace], 
                                  mode='markers',
                                  legendgroup="group2",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i> sec" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  showlegend = True, 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name= 'T<sub>1</sub> (mts)',
                                  marker_symbol=symbols[5],
                                  marker_color="rgb"+str(tableau20[3])))
        
    else: 
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_MTS_t1_matrix[trace], 
                                  mode='markers',
                                  legendgroup="group2",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i> sec" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  showlegend = False, 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='T<sup>1</sup> (mts)',
                                  marker_symbol=symbols[5],
                                  marker_color="rgb"+str(tableau20[3])))

# Add MEAN ------ nAn ------ MTR
for trace in range(0, len(mean_mtr_matrix)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    
    if trace == 0: 
    
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_mtr_matrix[trace], 
                                  mode='markers',
                                  visible=False,
                                  showlegend = True, 
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i>" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='MTR',
                                  marker_color="rgb"+str(tableau20[0])))
    else: 
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_mtr_matrix[trace], 
                                  mode='markers',
                                  visible=False,
                                  showlegend = False, 
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i>" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='MTR',
                                  marker_color="rgb"+str(tableau20[0])))
            

# Add MEAN ------ nAn ------ MTsat
for trace in range(0, len(mean_mtsat_matrix)):
    t = [trace -0.2 + i*0.14 for i in range(0, 4)]
    
    if trace == 0: 
    
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_mtsat_matrix[trace], 
                                  mode='markers',
                                  visible=False,
                                  showlegend = True, 
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i>" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='MTsat',
                                  marker_color="rgb"+str(tableau20[0])))
        
    else: 
        figb.add_trace(go.Scatter(x=t, 
                                  y=mean_mtsat_matrix[trace], 
                                  mode='markers',
                                  visible=False,
                                  showlegend = False, 
                                  legendgroup="group1",
                                  hovertemplate = 
                                  "Mean : <i> %{y: .2f} </i>" + 
                                  "<br>" + 
                                  "<b>%{text}</b>", 
                                  text = ['Session {}'.format(i + 1) for i in range(4)],
                                  name='MTsat',
                                  marker_color="rgb"+str(tableau20[0])))
        

# Calculate means 
line_1   = get_mean(mean_MP2RAGE_t1_matrix)    # MP2RAGE_t1 --- mean 

line_2   = get_mean(mean_MTS_t1_matrix)        # MTS_t1     --- mean 

line_3   = get_mean(mean_mtr_matrix)           # MTR   --- mean

line_4   = get_mean(mean_mtsat_matrix)         # MTsat  --- mean



# Add dotted lines for first button 
figb.add_trace(go.Scatter(x=[-1, 0, 1, 2, 3, 4, 5, 6], 
                          y=[line_1]*8,
                          mode='lines',
                          name='T<sub>1</sub>(mp2rage) mean',
                          opacity=0.5, 
                          line=dict(color="rgb"+str(tableau20[0]), 
                                    width=2,
                                    dash='dot')))

figb.add_trace(go.Scatter(x=[-1, 0, 1, 2, 3, 4, 5, 6], 
                          y=[line_2]*8,
                          mode='lines',
                          name='T<sub>1</sub>(mts) mean',
                          opacity=0.5, 
                          line=dict(color="rgb"+str(tableau20[3]), 
                                    width=2,
                                    dash='dot')))

# Add dotted lines for second button 
figb.add_trace(go.Scatter(x=[-1, 0, 1, 2, 3, 4, 5, 6], 
                          y=[line_3]*8,
                          mode='lines',
                          visible=False,
                          name='MTR mean',
                          opacity=0.5, 
                          line=dict(color="rgb"+str(tableau20[0]), 
                                    width=2,
                                    dash='dot')))


# Add dotted lines for thrid button 
figb.add_trace(go.Scatter(x=[-1, 0, 1, 2, 3, 4, 5, 6], 
                          y=[line_4]*8,
                          mode='lines',
                          visible=False,
                          name='MTsat mean',
                          opacity=0.5, 
                          line=dict(color="rgb"+str(tableau20[0]), 
                                    width=2,
                                    dash='dot')))



x = [-1, 0, 1, 2, 3, 4, 5, 6]
x_rev = x[::-1]

std_1   = get_std(mean_MP2RAGE_t1_matrix)   # MP2RAGE_t1      --- std 

std_2   = get_std(mean_MTS_t1_matrix)       # MTS_t1          --- std 

std_3   = get_std(mean_mtr_matrix)          # N/A_mtr         --- std

std_4   = get_std(mean_mtsat_matrix)        # MTS_mtsat       --- std



# Add STD for 1 button
figb.add_trace(go.Scatter(
    x=x+x_rev,
    y=[line_1+std_1]*8+[line_1-std_1]*8,
    fill='toself',
    fillcolor='rgba(31, 119, 180,0.15)',
    line_color='rgba(255,255,255,0)',
    showlegend=False,
    hoverinfo='skip',
    name='MP2RAGE_t1 STD',
))

figb.add_trace(go.Scatter(
    x=x+x_rev,
    y=[line_2+std_2]*8+[line_2-std_2]*8,
    fill='toself',
    fillcolor='rgba(255, 187, 120,0.15)',
    line_color='rgba(255,255,255,0)',
    showlegend=False,
    hoverinfo='skip',
    name='MTS_t1 STD',
))

# Add STD for 2 button
figb.add_trace(go.Scatter(
    x=x+x_rev,
    y=[line_3+std_3]*8+[line_3-std_3]*8,
    fill='toself',
    visible=False,
    fillcolor='rgba(31, 119, 180,0.15)',
    line_color='rgba(255,255,255,0)',
    showlegend=False,
    hoverinfo='skip',
    name='MTR STD',
))

# Add STD for 3 button
figb.add_trace(go.Scatter(
    x=x+x_rev,
    y=[line_4+std_4]*8+[line_4-std_4]*8,
    fill='toself',
    visible=False,
    fillcolor='rgba(31, 119, 180,0.15)',
    line_color='rgba(255,255,255,0)',
    showlegend=False,
    hoverinfo='skip',
    name='MTsat STD',
))



figb.update_layout(title = 'Brain white matter qMRI microstructure',
                   updatemenus=[
                                dict(
                                    active = 0, 
                                    x=1.23,
                                    y=0.58,
                                    direction="down",
                                    yanchor="top",
                                    buttons=list([
                                        dict(label="T<sub>1</sub>",
                                                     method="update",
                                                     args=[{"visible": [True] + [True]*12 + [False]*12 + [True]*2 + [False]*2 + [True]*2 + [False]*2},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(np.append(mean_MP2RAGE_t1_matrix, mean_MTS_t1_matrix, axis=0)), get_limit_max(np.append(mean_MP2RAGE_t1_matrix, mean_MTS_t1_matrix, axis=0))],
                                                                          title='T<sub>1</sub> [s]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                        
                                        dict(label="MTR",
                                                     method="update",
                                                     args=[{"visible": [True] + [False]*12 + [True]*6 + [False]*6 + [False]*2 + [True]*1 +[False]*1 + [False]*2 + [True]*1 +[False]*1},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(mean_mtr_matrix), get_limit_max(mean_mtr_matrix)],
                                                                          title='MTR [a.u.]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]),
                                        
                                        dict(label="MTsat",
                                                     method="update",
                                                     args=[{"visible":  [True] + [False]*18 + [True]*6 + [False]*3 + [True]*1 + [False]*3 + [True]*1},
                                                           
                                                           {"yaxis": dict(range=[get_limit_min(mean_mtsat_matrix),get_limit_max(mean_mtsat_matrix)],
                                                                          title='MTsat [a.u.]',
                                                                          mirror=True,
                                                                          ticks='outside', 
                                                                          showline=True, 
                                                                          linecolor='#000',
                                                                          tickfont = dict(size=y_label_tick_font))}]) ]) )],
                  title_x = 0.445, 
                  legend=dict(orientation = 'v',
                              bordercolor="Gray",
                              borderwidth=1),
                  xaxis=dict(range=[-0.45,5.45], 
                             mirror=True,
                             ticks='outside',
                             showline=True,
                             linecolor='#000',
                             tickvals = [0, 1, 2, 3, 4, 5],
                             ticktext = labels_subjects,
                             tickfont = dict(size=x_label_tick_font)),
                  yaxis_title='T<sub>1</sub> [s]',
                  yaxis=dict(range=[get_limit_min(np.append(mean_MP2RAGE_t1_matrix, mean_MTS_t1_matrix, axis=0)), get_limit_max(np.append(mean_MP2RAGE_t1_matrix, mean_MTS_t1_matrix, axis=0))], 
                             mirror=True,
                             ticks='outside', 
                             showline=True, 
                             linecolor='#000',
                             tickfont = dict(size=y_label_tick_font)),
                   annotations=[
                               dict(text="Display metric: ", 
                                     showarrow=False,
                                     x=1.25,
                                     y=0.62,
                                     xref = 'paper',
                                     yref="paper")],
                  plot_bgcolor='rgba(227,233,244, 0.5)',
                  width = 760, 
                  height = 520,
                  font = dict(size = general_font),
                  margin=go.layout.Margin(l=50,
                                         r=50,
                                         b=60,
                                        t=35))
# Plot figure
# For jupyter-book rendering --=-- jupyter-lab
plot(figb, filename = 'new-fig-4.html', config = config)
display(HTML('new-fig-4.html'))
# For local jupyter notebook --== binder session
# iplot(figb,config=config)
/tmp/ipykernel_1863/3892547858.py:5: DeprecationWarning:

Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display